package com.itguigu.glimail.oauth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itguigu.common.utils.R;
import com.itguigu.glimail.oauth.feign.MemberFeignService;
import com.itguigu.glimail.oauth.vo.MemberRespVo;
import com.itguigu.glimail.oauth.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;

/**
 * 处理社交登陆请求
 */
@Controller
@CrossOrigin
public class Oauth2Controller {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    MemberFeignService memberFeignService;
    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据code 获取accessToken
        //https://api.weibo.com/oauth2/access_token
        // ?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code
        // &redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE

//        Map<String, String> map = new HashMap<>();
        MultiValueMap map = new LinkedMultiValueMap();
        map.add("client_id", "263695028");
        map.add("client_secret", "ae8dece260d8553630dd142ab1f73e03");
        map.add("grant_type", "authorization_code");
        map.add("redirect_uri", "http://auth.glimail.com/oauth2.0/weibo/success");
        map.add("code", code);
//        HttpResponse post = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", null, null, map);
//        String s = post.getEntity().toString();
//        System.out.println(s);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("https://api.weibo.com/oauth2/access_token", map, String.class);
        if (responseEntity.getStatusCode().value()==200) {
            String body = responseEntity.getBody();
            SocialUser socialUser = JSON.parseObject(body, SocialUser.class);
            //知道当前是哪个社交用户
            //当用户第一次进网站,自动注册进来(为当前用户生产一个会员信息账号,以后这个社交账号就关联这个账号)
            //登录或者注册
            R r = memberFeignService.oauthLogin(socialUser);
            if (r.getCode() == 0) {
                MemberRespVo data = r.getData("data", new TypeReference<MemberRespVo>() {
                });

                System.out.println("登录成功:"+data.toString());
                //第一次使用session ,命令浏览器保存卡号  JSESSIONID这个cookie
                //以后浏览器访问哪个网站就会带上这个网站的cookie
                //子域之间  glimail.com auth.glimail.com  order.glimail.com
                //发卡的时候指定域名为父域名 即使是子域发的卡 也能让父域使用
                //todo 默认发的令牌 session = dfsfsd 作用域是当前域(解决子域session共享问题)
                //todo 使用json序列化的方式来序列化对象到redis
                 session.setAttribute("loginUser", data);

                return "redirect:http://glimail.com";
            } else {
                return "redirect:http://auth.glimail.com/login.html";
            }


        }else {
            return "redirect:http://auth.glimail.com/login.html";
        }


        //处理
        //登录成功跳回首页
//        return "redirect:http://auth.glimail.com";
    }

}
